package lib.com.Service;

import lib.com.Business.component.register.RegisterComponent;
import lib.com.Business.dataModel.RegisterModel;
import lib.com.Service.In.Core0002In;
import lib.com.Service.Out.Core0002Out;
import water.com.Ioc.WaterBean;
import water.com.Ioc.WaterInject;
import water.com.Service.base.BaseCore;
import water.com.Service.entity.comm.SysHead;


/**
 * 普通用户注册功能
 */
@WaterBean(type = "Service")
public class Core0002 implements BaseCore<Core0002In, Core0002Out> {
    @WaterInject
    private RegisterComponent registerComponent;

    @Override
    public Core0002Out execute(Core0002In core0002In) throws Exception {
        Core0002Out core0002Out = new Core0002Out();
        core0002Out.setBody(new Core0002Out.Body());
        SysHead sysHead = core0002In.getSysHead();
        RegisterModel registerModel = new RegisterModel();
        Core0002In.Body body= core0002In.getBody();
        registerModel.setAccount(body.getAccount());
        registerModel.setPassword(body.getPassword());
        registerModel.setUserName(body.getUserName());
        registerModel.setPhone(body.getPhone());
        if(registerComponent.UserRegister(registerModel)){
            sysHead.setResponseCode("000000");
            core0002Out.getBody().setRegisterFlag("Y");
        }else {
            sysHead.setResponseCode("999999");
            core0002Out.getBody().setRegisterFlag("N");
            core0002Out.getBody().setFiledInfo("该账号已存在或存在其他错误");
        }
        core0002Out.setSysHead(sysHead);
        return core0002Out;
    }

    @Override
    public void check(Core0002In core0002In) throws Exception {
        if(core0002In.getBody().getAccount()==null||core0002In.getBody().getAccount().trim().equals("")){
            throw new Exception("check:账号不能为空");
        }
        if(core0002In.getBody().getPassword()==null||core0002In.getBody().getPassword().trim().equals("")){
            throw new Exception("check:密码不能为空");
        }
        if(core0002In.getBody().getPhone()==null||core0002In.getBody().getPhone().trim().equals("")){
            throw new Exception("check:联系方式不能为空");
        }
        if(core0002In.getBody().getUserName()==null||core0002In.getBody().getUserName().trim().equals("")){
            core0002In.getBody().setUserName(String.valueOf(System.currentTimeMillis()));
        }
        RegisterModel registerModel = new RegisterModel();
        registerModel.setAccount(core0002In.getBody().getAccount());
        if(!registerComponent.UserRegisterCheck(registerModel)){
            throw new Exception("check:该账号已被注册，请重新输入账号");
        }

    }
}
